<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>选择接收对象</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<script src="js/alai_tree/alai_tree.js"></script>
	<script src="js/alai_tree/alai_tree_pretty.js"></script>
	<script src="js/alai_tree/alai_tree_check.js"></script>
	<script src="js/prototype-1.4.0.js"></script>
	<script src="js/InputTool.js"></script>
<link href="<%=path%>/template/css.css" rel="stylesheet" type="text/css">
	<link href="<%=path%>/css/style.css" rel="stylesheet" type="text/css">

  
  </head>
  
  <body onload="proLoad();">
    <div id="divTree1" style="overflow: auto;width: 330;height: 400;float: none;"></div>
    <hr>
    <input class="submitbtn" type='button' onclick="returnTime()" value="确定" >&nbsp;<input type='button' class="backbtn" onclick="clearTime()" value="取消" >
  </body>
</html>
<script>
var tree1=new alai_tree_pretty(divTree1)
var root=tree1.root
	function proLoad(){
		//var url= dialogArguments.url;
		var inputObj=dialogArguments.input;
		//初始化树
		loadTreeNode(root);
	}
	
	function loadTreeNode(pidNode){
		var pid="-1";
		if(pidNode.body)pid=pidNode.body.pid
		var url="<%=request.getContextPath()%>/departmenttreenode.action?pid="+pid;
		var myAjax = new Ajax.Request(
					url, 
					{ 
						method: 'get', 
						asynchronous:false
				}); 
		var myXML = myAjax.transport.responseXML;
		addNodeToTree(myXML,pidNode);
	}
	
	function addNodeToTree(doc,pidNode){
		var nodes=doc.selectNodes("TREENODES/TREENODE");
		if(nodes.length>0){
			var node,treeNode;
			for(var i=0;i<nodes.length;i++){
				node=nodes[i];
				//treeNode=pidNode.add(node.getAttribute('name'));
				treeNode=tree1.addChkNode(pidNode,node.getAttribute('name'));
				
				treeNode.expand(false);
				treeNode.body.flg=0;
				treeNode.body.pid=node.getAttribute('id')
				treeNode.checkBox.checked=false;
				
				
				treeNode.icon.src='js/alai_tree/images/fold_close.gif'
			}
			//treeNode=tree1.addChkNode(pidNode,"cesh");
			//loadUserNode(pidNode)
		}
	}
	function treeClkListener(){
	tree1.onclick=function(treeNode){
		var flg=treeNode.body.flg;
		if(0==flg){
			loadTreeNode(treeNode);
			loadUserNode(treeNode);
			treeNode.body.flg=1;
			treeNode.expand();
		}
		treeNode.expand();
		if(treeNode.expanded)treeNode.icon.src='js/alai_tree/images/fold_open.gif'
		//setFun();
	}
}
function loadUserNode(pidNode){
		var pid="-1";
		if(pidNode.body)pid=pidNode.body.pid
		var url="<%=request.getContextPath()%>/departmentuserlist.action?pid="+pid;
		var myAjax = new Ajax.Request(
					url, 
					{ 
						method: 'get', 
						asynchronous:false
				}); 
		var myXML = myAjax.transport.responseXML;
		addUserToTree(myXML,pidNode);
	}
	
	function addUserToTree(doc,pidNode){
		var nodes=doc.selectNodes("TREENODES/TREENODE");
		if(nodes.length>0){
			var node,treeNode;
			for(var i=0;i<nodes.length;i++){
				node=nodes[i];
				//treeNode=pidNode.add(node.getAttribute('name'));
				treeNode=tree1.addChkNode(pidNode,node.getAttribute('name'));
			
				treeNode.expand(false);
				treeNode.body.flg=1;
				treeNode.body.pid=node.getAttribute('id')
				treeNode.checkBox.name="mobiles";
				treeNode.checkBox.value=node.getAttribute('id');
				treeNode.checkBox.usermobile=node.getAttribute('mobile');
				treeNode.checkBox.username=node.getAttribute('name');
				treeNode.checkBox.checked=false;
				 
				
				//treeNode.icon.src='js/alai_tree/images/fold_close.gif'
			}			
			//treeNode=tree1.addChkNode(pidNode,"cesh");
			//loadUserNode(pidNode)
		}
	}
treeClkListener()

var selNodes=new Array();
function isExist(srcNode){
	var node;
	for(var i=0;i<selNodes.length;i++){
		node=selNodes[i];
		if(node.checkBox.value==srcNode.checkBox.value)return true;
	}
	return false;
}

function addA(srcNode){
	if(!isExist(srcNode))
		selNodes.push(srcNode);
}

function delA(srcNode){
	var newArray=new Array();
	var node;
	
	for(var i=0;i<selNodes.length;i++){
		node=selNodes[i];
		if(node.checkBox.value!=srcNode.checkBox.value){
			newArray.push(node);	
		}
	}
	/*****/
	selNodes=newArray;
}

tree1.oncheck=function(srcNode)
	{
	if(srcNode.checkBox!=null){
	if(srcNode.checkBox.checked)     //   选中   
          {   
    
                          var   node1   =   srcNode.first;   
                          while   (node1   !=   null)   
                          {   
                                  node1.checkBox.checked   =   true;   
                                  node1   =   node1.next;   
                          }   
          }   
          else         //取消   
          {   
                          var   node1   =   srcNode.first;   
                          while   (node1   !=   null)   
                          {   
                                  node1.checkBox.checked   =   false;   
                                  node1   =   node1.next;   
                          }   
          }   
          }
		if(srcNode.checkBox){
			if(srcNode.checkBox.checked){
			if(srcNode.checkBox.username!=null){
				addA(srcNode);
			}
				 
			}else{
				delA(srcNode);
			}
		}else{
			
		}
	}
	function returnTime(){
		var rtnObj = new Object();
				
				
				var rs='';
				var rsname='';
				if(selNodes){
					var user;
					for(var i=0;i<selNodes.length;i++){
						user=selNodes[i];
						rs+=user.checkBox.usermobile+";";
						rsname+=user.checkBox.username+";";
						
					}
				}
				
				rtnObj.result =rs;
				rtnObj.resultname =rsname;
				returnValue = rtnObj;
				window.close();
	}
function clearTime(){
	
		window.close();
}
</script>